#!/bin/bash

time="$(date +%Y"-"%m"-"%d"_"%k":"%M":"%S)"
cd $(cd `dirname $0`; pwd)
java_dir=`pwd`
if [ ! -d "$java_dir" ]; then
        echo $time  $java_dir is not exits.exit. 
        exit -1;
fi
javaVersionOld=$(echo `java -version 2>&1 | awk 'NR==1{ gsub(/"/,""); print $3 }'`)
test "$javaVersionOld" == '1.7.0_79' && echo "java(1.7.0_79) is already installed!!" && exit 0 || echo "java 1.7.0_79 is not installed."

echo $time begin to install--------------------------java  
echo uninstall java low version
rpm -ev --nodeps `rpm -qa "java*"`

java_install_file=`ls $java_dir/jdk*`
if [ "$java_install_file"!="\n"  ]; then
        echo "begin to install $java_install_file"
        rpm -ivh $java_install_file
else
        echo "java_install_file not found,exit"
        exit -1
fi

java_version=$(echo `java -version 2>&1 | awk 'NR==1{ gsub(/"/,""); print $3 }'`)
echo "java version is $java_version"
java_home=/usr/java/jdk$java_version
if [ ! -d "$java_home" ]; then
        echo $time  $java_home is not exits.exit. 
        exit -1;
fi
echo "java home is $java_home"
echo "#OLD_PATH=$PATH" >>  /etc/profile
echo "#Java environment" >> /etc/profile
echo 'export JAVA_HOME=/usr/java/jdk1.7.0_79' >> /etc/profile
echo 'export JAVA_HOME' >> /etc/profile
echo '# java env end' >> /etc/profile
source /etc/profile


#Java environment
#export JAVA_HOME=/usr/java/jdk1.7.0_79
#export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
#export PATH=$JAVA_HOME/bin:$PATH
#export BX_LOG=/var/log/tomcat